package my.merborn.ordersysteam105.controllers;

import my.merborn.ordersysteam105.models.dao.MenuMapper;
import my.merborn.ordersysteam105.models.pojo.Menu;
import my.merborn.ordersysteam105.models.pojo.OrderT;
import my.merborn.ordersysteam105.models.pojo.User;
import my.merborn.ordersysteam105.models.services.MenuService;
import my.merborn.ordersysteam105.models.services.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class OrderFoodController {

    @Autowired
    MenuMapper mm;
    @Autowired
    MenuService ms;
    @Autowired
    OrderService os;

    /**
     * @param request
     * @return view's url
     * <p>
     * 用于向页面填充数据
     */
    @RequestMapping(value = "/foodmenu.html", method = RequestMethod.GET)
    public ModelAndView foodList(
            HttpServletRequest request,
            @RequestParam(required = false) Integer page,
            @RequestParam(required = false) ModelAndView mv
    ) {

        List<Menu> menuList;
        if (mv == null) {
            System.out.println("foodmenu.list :\n mv is null");
            mv = new ModelAndView("foodMenu.html");
            if (page != null) {
                System.out.println("foodmenu.list page!= null");
                menuList = ms.getALLByPageable(new PageRequest(page,5));
                mv.addObject("current", page);
            } else {
                menuList = ms.getALLByPageable(new PageRequest(0, 5));
                mv.addObject("current", 0);
            }
            mv.addObject("menuList", menuList);//传给页面使用
        }
        //List<Menu> menuList = ms.getALL();
        //ModelAndView mv = new ModelAndView("foodMenu.html");
        HttpSession session = request.getSession();
        String cart = (String) session.getAttribute("shoppingCart");
        mv.addObject("cart", cart);
        if (session.getAttribute("uid") != null) {
            mv.addObject("uid", session.getAttribute("uid"));
        }
        return mv;
    }


    @RequestMapping("/foodmenu.next")
    public ModelAndView foodMenuNext(HttpServletRequest request,@PageableDefault(size = 5,page = 0) Pageable pageable) {
        List<Menu> allByPageable = ms.getALLByPageable(pageable);
        ModelAndView mv = new ModelAndView("foodmenu.html");
        mv.addObject("current", pageable.getPageNumber());
        mv.addObject("menuList", allByPageable);

        HttpSession session = request.getSession();
        String cart = (String) session.getAttribute("shoppingCart");
        mv.addObject("cart", cart);
        if (session.getAttribute("uid") != null) {
            mv.addObject("uid", session.getAttribute("uid"));
        }

        return mv;
    }

    @RequestMapping("/foodmenu.last")
    public ModelAndView foodMenuLast(HttpServletRequest request,@PageableDefault(page = 0,size = 5) Pageable pageable) {
        List<Menu> allByPageable = ms.getALLByPageable(pageable);
        ModelAndView mv = new ModelAndView("foodmenu.html");
        mv.addObject("current", pageable.getPageNumber());
        mv.addObject("menuList", allByPageable);

        HttpSession session = request.getSession();
        String cart = (String) session.getAttribute("shoppingCart");
        mv.addObject("cart", cart);
        if (session.getAttribute("uid") != null) {
            mv.addObject("uid", session.getAttribute("uid"));
        }
        return mv;
    }

    /**
     * @param request
     * @return ModelandView
     * 用于处理点餐页面数据的填充
     */
    @RequestMapping(value = "/orderfood.do", method = RequestMethod.GET)
    public ModelAndView orderFood(@RequestParam(required = true) String uid, HttpServletRequest request) {
        ModelAndView mv = new ModelAndView();
        request.getSession().setAttribute("uid", uid);
        mv.setViewName("redirect:foodmenu.html");
        return mv;
    }

    /**
     * @param mid
     * @param request
     * @return 用于处理购物车
     */
    @RequestMapping(value = "/addmyshoppingcart", method = RequestMethod.GET)
    public ModelAndView addFoodIntoMyShoppingCart(
            @RequestParam(required = false) Integer page,
            @RequestParam(required = true) Integer mid,
            HttpServletRequest request) {

        HttpSession session = request.getSession();
        if (session.getAttribute("shoppingCart") == null) {
            session.setAttribute("shoppingCart", "");
        }
        String cart = session.getAttribute("shoppingCart") + "" + mid + "@";
        session.setAttribute("shoppingCart", cart);
        ModelAndView mv = new ModelAndView("redirect:foodmenu.html?page="+page);
//        List<Menu> menuList = ms.getALL();
//        mv.addObject("menuList",menuList);
        mv.addObject("mysession", cart);

        return mv;
    }

    @RequestMapping("/finishorder.do")
    public ModelAndView sendUserOrder(@RequestParam(required = true) String order, @RequestParam(required = true) String uid, HttpServletRequest request) {
        //System.out.println("order = " + order);
        ModelAndView mv = new ModelAndView();
        if (uid == null || "".equals(uid)) {
            System.out.println("id of error!");
            mv.setViewName("userlogin.html");
        } else {

            User user = new User();
            user.setuId(Integer.parseInt(uid));

            //根据购物车中的菜品生成订单！！！
            OrderT newOrder = os.createNewOrder(user, order);
            //完成订单后清空已点订单
            request.getSession().setAttribute("shoppingCart", "");

            mv.setViewName("redirect:paypage.html?uid=" + uid);
            //System.out.println(order);
        }
        return mv;
    }

//    @RequestMapping("/test.do")
//    public void teeee(HttpServletRequest request){
//        HttpSession session = request.getSession();
//        String shoppingCart = (String) session.getAttribute("shoppingCart");
//        Cookie cookie = null;
//        Cookie[] cookies = request.getCookies();
//        for (Cookie c : cookies) {
//                if(c.getName().equals("JSESSIONID")){
//                   cookie = c;
//                    break;
//                }
//        }
//
//        if(cookie.getValue().equals(session.getId())){
//            System.out.println("he fa");
//        }
//    }

}
